#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
#include <assert.h>
#include <time.h>
#include <errno.h>

#define TEST_1

#ifdef TEST_1

#define TH1_PR 30
#define TH1_SCH SCHED_OTHER

#define TH2_PR 30
#define TH2_SCH SCHED_OTHER

#define TH3_PR 30
#define TH3_SCH SCHED_OTHER

#endif


#if 1
void *Thread1(void* pp)
{
  sleep(1);
  int i;
  int policy;
  struct sched_param param;
  pthread_getschedparam(pthread_self(),&policy,&param);
  switch(policy)
  {
  case SCHED_OTHER:
      printf("SCHED_OTHER 1\n");
      break;

  case SCHED_RR:
      printf("SCHED_RR 1 \n");
      break;

  case SCHED_FIFO:
      printf("SCHED_FIFO 1\n");
      break;
  }

while(1)
{
    for(i=1;i<5000000;i++)
    {


    }
    printf("Pthread 1\n");
    usleep(500000);
}
  printf("Pthread 1 exit\n");
}

void *Thread2(void* pp)
{
  sleep(2);
  int i;
  int policy;
  struct sched_param param;
  pthread_getschedparam(pthread_self(),&policy,&param);
    switch(policy)
  {
  case SCHED_OTHER:
      printf("SCHED_OTHER 2\n");
      break;

  case SCHED_RR:
      printf("SCHED_RR 2 \n");
      break;

  case SCHED_FIFO:
      printf("SCHED_FIFO 2\n");
      break;
  }
while(1)
{
    for(i=1;i<5000000;i++)
    {


    }
    printf("Pthread 2\n");
    usleep(500000);
}
  printf("Pthread 2 exit\n");
}

void *Thread3(void* pp)
{
  sleep(3);
  int i;
  int policy;
  struct sched_param param;
  pthread_getschedparam(pthread_self(),&policy,&param);
    switch(policy)
  {
  case SCHED_OTHER:
      printf("SCHED_OTHER 3\n");
      break;

  case SCHED_RR:
      printf("SCHED_RR 3 \n");
      break;

  case SCHED_FIFO:
      printf("SCHED_FIFO 3\n");
      break;
  }
while(1)
{
    for(i=1;i<5000000;i++)
    {


    }
    printf("Pthread 3\n");
    usleep(500000);
}
  printf("Pthread 3 exit\n");
}
#endif

int main()
{
    int uid;
    uid = getuid();
    if(uid==0)
        printf("The current user is root\n");
    else
        printf("The current user is not root\n");

    pthread_t ppid1,ppid2,ppid3;

    struct sched_param param1;
    struct sched_param param2;
    struct sched_param param3;

    pthread_attr_t attr1,attr2,attr3;

    pthread_attr_init(&attr1);
    pthread_attr_init(&attr2);
    pthread_attr_init(&attr3);

    // param1 -- SCHED_OTHER  prio -- 40
    param1.sched_priority = TH1_PR;
    // 优先级
    pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED);
    // 调度策略
    pthread_attr_setschedpolicy(&attr1, TH1_SCH);
    int ret1 = pthread_attr_setschedparam(&attr1,&param1);
    if(0!=ret1)
    {
      printf("setschedpolicy attr1 error, no:%d, ret1:%d \n", errno, ret1);
    }

    // param2 -- SCHED_RR  prio -- 20
    param2.sched_priority = 20;
    pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED);
    pthread_attr_setschedpolicy(&attr2,SCHED_RR);
    pthread_attr_setschedparam(&attr2,&param2);
    int ret2 = pthread_attr_setschedparam(&attr2,&param2);
    if(0!=ret2)
    {
      printf("setschedpolicy attr2 error, no:%d, ret2:%d \n", errno, ret2);
    }

    // param3 -- SCHED_RR  prio -- 20
    param3.sched_priority = ;
    pthread_attr_setinheritsched(&attr3,PTHREAD_EXPLICIT_SCHED);
    pthread_attr_setschedpolicy(&attr3,SCHED_RR);
    pthread_attr_setschedparam(&attr3,&param3);

    int ret3 = pthread_attr_setschedparam(&attr3,&param3);
    if(0!=ret3)
    {
      printf("setschedpolicy attr3 error, no:%d, ret3:%d \n", errno, ret3);
    }

    pthread_create(&ppid1,&attr1,Thread1,NULL);

    pthread_create(&ppid2,&attr2,Thread2,NULL);

    pthread_create(&ppid3,&attr3,Thread3,NULL);

    pthread_join(ppid1,NULL);
    pthread_join(ppid2,NULL);
    pthread_join(ppid3,NULL);
    //while(1) {
        //sleep(100);
        //printf("main loop\n");
    //}

    pthread_attr_destroy(&attr1);
    pthread_attr_destroy(&attr2);
    pthread_attr_destroy(&attr3);
    return 0;
}
